/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cl.whyem.service;

import cl.whyem.dao.SomeItem;
import cl.whyem.dao.SomeItemRepository;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("someItemService")
public class SomeItemServiceImpl implements SomeItemService {

    @Autowired
    private SomeItemRepository someItemRepository;

    @PostConstruct
    @Transactional
    public void populate() {
        SomeItem si = new SomeItem();
        si.setSomeText("aaa");
        someItemRepository.saveAndFlush(si);

        si = new SomeItem();
        si.setSomeText("bbb");
        someItemRepository.saveAndFlush(si);

        si = new SomeItem();
        si.setSomeText("ccc");
        someItemRepository.saveAndFlush(si);
    }

    @Transactional(readOnly = true)
    @Override
    public List<SomeItem> getAll() {
        return someItemRepository.findAll();
    }

    @SuppressWarnings("AssignmentToMethodParameter")
    @Transactional
    @Override
    public SomeItem saveAndFlush(SomeItem si) {
        if (si != null) {
            si = someItemRepository.saveAndFlush(si);
        }
        return si;

    }

    @Transactional
    @Override
    public void delete(long id) {
        someItemRepository.delete(id);
    }
}
